
package com.itluma.isWOak.controller;

import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.pojo.RoleInfo;
import com.itluma.isWOak.service.RoleService;
import com.itluma.isWOak.vo.RolesForm;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author ms
 * @create 2021-04-11 10:07
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    //日志文件
    private Logger log = LoggerFactory.getLogger (RoleController.class);

    @Autowired
    private RoleService roleService;


    @RequiresPermissions ("role:list")
    @GetMapping("/role_list")
    public String page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit, Model model) {

        PageInfo<RoleInfo> pageInfo = roleService.page(page, limit);

        log.info ("进入页面展示数据: " + pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "role/role_list";

    }
    /**
     * 增加
     * @return
     */
    @RequiresPermissions ("role:add")
    @GetMapping("/role_add")
    public ModelAndView addShow(){

        log.info ("进入role add界面 ");
        ModelAndView mv = new ModelAndView ();
        mv.setViewName ("role/role_add");
        return mv;
    }

    @RequiresPermissions ("role:add")
    @PostMapping("/role_add")
    @ResponseBody
    public Boolean add(RolesForm rolesForm){
        log.info ("获取角色add提交的数据:  " +rolesForm);

        boolean add =  roleService.add(rolesForm);
        return Boolean.valueOf (add);
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @RequiresPermissions("role:modi")
    @GetMapping("/role_modi")
    public ModelAndView modifyShow(String rid){

        RoleInfo roleInfo = roleService.getByRid (rid);

        log.info ("进入修改界面: " + roleInfo);
        ModelAndView mv = new ModelAndView ();
        mv.setViewName ("role/role_modi");
        mv.addObject ("roleInfo", roleInfo);
        return mv;
    }

    @RequiresPermissions ("role:modi")
    @PostMapping("/role_modi")
    @ResponseBody
    public Boolean modify(RolesForm rolesForm){

        log.info ("进入修改表单提交页面: " + rolesForm);

        boolean update =  roleService.update(rolesForm);
        return Boolean.valueOf (update);
    }

    @RequiresPermissions("role:delete")
    @GetMapping("/role_delete")
    public ModelAndView delete(String rid){

        log.info ("进入删除页面!");
        ModelAndView mv = new ModelAndView ();
        Boolean delete = roleService.deleteByRid (rid);
        mv.addObject ("delete",delete);
        mv.setViewName ("redirect:/role/role_list");
        return mv;
    }
}